var express = require('express');
var router = express.Router();
const addressService = require('../service/addressService');

// 查询所有地址 
router.get('/', async function (req, res, next) {
    let { type, value, current, size } = req.query;
    let searchObj = {}; //
    if (type) {
        if (type == "phone") {
            searchObj[type] = value;
        } else {
            searchObj[type] = { $regex: value };
        }
    }
    let data = await addressService.findAllAddress(searchObj, { current: current || 1, size: size || 10 });
    res.send(data);
});

// 新增地址
router.post('/', async function (req, res, next) {
    let data = await addressService.addNewAddress(req.body);
    res.send(data);
});

// 根据 id 删除地址
router.delete('/:id', async function (req, res, next) {
    let id = req.params.id;
    let data = await addressService.deleteAddress(id)
    res.send(data);
});

module.exports = router;